package hottargui.standard;

import hottargui.framework.Game;
import hottargui.framework.GameData;
import hottargui.framework.IllegalActionException;
import hottargui.framework.Player;
import hottargui.framework.PlayerColor;
import hottargui.framework.Position;
import hottargui.framework.State;
import hottargui.framework.TarguiState;
import hottargui.framework.TurnSequenceStrategy;

public class StandardEndRoundState implements TarguiState {

	Game gameContext;

	public StandardEndRoundState(Game game) {
		this.gameContext = game;
	}

	public boolean buy(int count, Position deploy)
			throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public State getState() {
		return State.endRound;
	}

	public PlayerColor getWinner() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public boolean move(Position from, Position to, int count)
			throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public void newGame() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public void rollDie() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public PlayerColor turnCard() throws IllegalActionException {
		GameData gd = gameContext.getGameObjectServer().getGameData();
    	for (Player player : gd.getPlayers().values()) {
    		((StandardPlayer) player).add(gd.getRevenue(player));
    	}

		if (gd.getRoundsLeft() == 0) {
			gameContext.setState(gameContext.getFactory().getEndGameState(gameContext));
			return PlayerColor.None;
		}
		
		TurnSequenceStrategy tss = gameContext.getGameObjectServer().getTurnSequenceStrategy();
		tss.startNewTurn(gd.getLastDieRoll());
		gd.decreaseRoundsLeft();
		gd.setPlayerInTurn(tss.nextPlayer());
		gameContext.setState(gameContext.getFactory().getMoveState(gameContext));
		
		return gd.getPlayerInTurn().getColor();
	}

	public void yieldAttack() throws IllegalActionException
	{
		throw new IllegalActionException(this.getState());
	}
	
	public void skip() throws IllegalActionException
	{
		throw new IllegalActionException(this.getState());
	}
}
